﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace black_screen_mode_server.Models
{
    public class CurrentGroupScreenInfos
    {
        public int GroupId { get; set; }

        public ScreenStatus Screen_Status { get; set; }
        //{
        //    get 
        //    { 
        //        return screen_Status;
        //    }
        //    set
        //    {
        //        if(value != this.Screen_Status)
        //        {
        //            this.screen_Status = value;
                    
        //        }
        //    }
        //}


       // private DateTime latestAlarmTime;


        //private string Action { get; set; }
        //private string Source { get; set; }
        //private int Category { get; set; }
        //private Int64 TransactionId { get; set; }


        public QueryEventValues QueryEventValues { get; set; }

        public BlackScreenExtraInfo blackScreenExtraInfo { get; set; }


        public void JudgeScreenStatus(QueryEventValues latestValues, BlackScreenExtraInfo info)
        {
           //更新所有参数
           //判断黑屏状态
            UpdateParameters(latestValues, info);
            JudgementScreenState(latestValues.LocalTime);
        }


        private void JudgementScreenState(DateTime latestAlarmTime)
        {
            Logger.Singleton.Info($"开始判断黑屏状态：=====================================");
            Logger.Singleton.Info($"最新报警时间：{latestAlarmTime.ToString("yyyy-MM-dd HH:mm:ss")}");
            Logger.Singleton.Info($"当前时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            if (DateTime.Now >= latestAlarmTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60))
            {
                this.Screen_Status = ScreenStatus.black_screen;
                Logger.Singleton.Info($"当前黑屏状态：黑屏");
            }
            else
            {
                this.Screen_Status = ScreenStatus.white_screen;
                Logger.Singleton.Info($"当前黑屏状态：白屏");
            }
            
        }

        public void RefreshScreenStatus()
        {
            //其余报警正常计算黑屏状态
            if (DateTime.Now >= this.QueryEventValues.LocalTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60))
            {
                this.Screen_Status = ScreenStatus.black_screen;
            }
            else
            {
                this.Screen_Status = ScreenStatus.white_screen;
            }
        }   

        //更新参数
        private void UpdateParameters(QueryEventValues latestValues, BlackScreenExtraInfo info)
        {
          //将latestValues的所有字段值赋给QueryEventValues的字段
            this.QueryEventValues.Action = latestValues.Action;
            this.QueryEventValues.Source = latestValues.Source;
            this.QueryEventValues.Category = latestValues.Category;
            this.QueryEventValues.TransactionID = latestValues.TransactionID;
            this.QueryEventValues.LocalTime = latestValues.LocalTime;
            this.QueryEventValues.SourceSort = latestValues.SourceSort;
            this.QueryEventValues.AreaCode = latestValues.AreaCode;
            this.QueryEventValues.Priority = latestValues.Priority;
            this.QueryEventValues.ConditionName = latestValues.ConditionName;
            this.QueryEventValues.Block = latestValues.Block;
            this.QueryEventValues.Description = latestValues.Description;
            this.QueryEventValues.LocationTagName = latestValues.LocationTagName;
            this.QueryEventValues.SourceEntityName = latestValues.SourceEntityName;
            //将info的所有字段值赋给blackScreenExtraInfo的字段
            this.blackScreenExtraInfo.source_sort = info.source_sort;
            this.blackScreenExtraInfo.source = info.source;
            this.blackScreenExtraInfo.Category = info.Category;
            this.blackScreenExtraInfo.area_code = info.area_code;
            this.blackScreenExtraInfo.priority = info.priority;
            this.blackScreenExtraInfo.condition_name = info.condition_name;
            this.blackScreenExtraInfo.block = info.block;
            this.blackScreenExtraInfo.description = info.description;
            this.blackScreenExtraInfo.location_tag_name = info.location_tag_name;
            this.blackScreenExtraInfo.station = info.station;
            this.blackScreenExtraInfo.groupName = info.groupName;
            this.blackScreenExtraInfo.category_name = info.category_name;
        }

        //// 1.如果最新时间 >= 最近报警时间 + 延迟时间，则当前为黑屏；
        //// 2.否则为白屏。
        //// 如果为黑屏，黑屏开始时间 = 最近报警时间 + 延迟时间
        //// 如果为白屏，白屏开始时间 = 最近报警时间,也为黑屏结束时间
        //public DateTime LatestAlarmTime { get; set; }
        //{
        //    get
        //    { 
        //        return latestAlarmTime;
        //    }
        //    set
        //    {
        //        this.latestAlarmTime = value;
        //        if (DateTime.Now >= this.LatestAlarmTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60))
        //        {
        //            this.Screen_Status = ScreenStatus.black_screen;
        //        }
        //        else
        //        {
        //            this.Screen_Status = ScreenStatus.white_screen;
        //        }
        //    }
        //}

        
        public CurrentGroupScreenInfos(int groupId)
        {
            this.GroupId = groupId;
            this.Screen_Status = ScreenStatus.white_screen;
            this.QueryEventValues = new QueryEventValues();
            this.blackScreenExtraInfo = new BlackScreenExtraInfo();
        }
    }
}
